package com.le.tester.book.flightjava8.parameterpassing;

import java.util.ArrayList;
import java.util.List;

/**
 * createTime：2025-06-19 11:12
 * description：筛选苹果
 */
public class AppleSelector {

    //1 version
    public static List<Apple> filterGreen(List<Apple> appleList) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple item : appleList) {
            if ("green".equals(item.getColor())) {
                result.add(item);
            }
        }
        return result;
    }

    //2 version
    public static List<Apple> filterAppleByColor(List<Apple> appleList, String color) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple item : appleList) {
            if (color.equals(item.getColor())) {
                result.add(item);
            }
        }
        return result;
    }

    //3 version
    public static List<Apple> filterApples(List<Apple> appleList, String color, int weight, boolean flag) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple item : appleList) {
            //low
            if ((flag && color.equals(item.getColor()))
                    || (!flag && weight < item.getWeight())) {
                result.add(item);
            }
        }
        return result;

    }

    //4 抽象化
    public static List<Apple> filterApples(List<Apple> appleList, AppleHandler appleHandler) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple item : appleList) {
            if (appleHandler.test(item)) {
                result.add(item);
            }
        }
        return result;
    }

    public static void main(String[] args) {

        //java 8调用新写法
        filterApples(new ArrayList<Apple>(), apple -> false);
    }

}
